//
//  CHSViewController.m
//  ZXMBProgressHUD
//
//  Created by 海参 on 09/30/2022.
//  Copyright (c) 2022 海参. All rights reserved.
//

#import "CHSViewController.h"
#import <ZXMBProgressHUD/ZXMBProgressHUD.h>
#import "CHSHeaderTitleView.h"


@interface CHSViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, copy) NSArray *dataArray;

@end

@implementation CHSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUI];
    [self setData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setUI{
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    [self.tableView registerClass:[CHSHeaderTitleView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([CHSHeaderTitleView class])];
    NSLog(@"打印台测试是否可打印");
}

#pragma mark - Data

- (void)setData
{
    //组数据
    NSArray *sectionArr = @[
        @{
            @"section":@"selector",
            @"sectionTitle":@"此SDK的优点：\n1）window处理，其它二封的库普遍用keyWindow，其实会有bug，keyWindow有时候会被意外改变，导致无法展示HUD，或者无法移除HUD。\n2）做了loading防止重复加载处理，如果当前正在loading，新加载的loading直接用当前loading的UI，只修改文本。\n3) 支持iOS13 SDK的UIWindowScene；\n4) 配置了：显示loading的宽限时间,0.1s；\n5)支持toast展示时间根据文本长度动态计算。再配置最小显示持续时间1.5s；再限制最大展示时间6s；\n6)支持completionBlock隐藏移除时的block回调；有些业务需要隐藏后再处理其它事件。\n7)支持线程安全，即使子线程调用也会提交到主线程队列执行。",
            @"list":@[
              @{
                  @"tag":@"loading1",
                  @"title":@"展示loading：默认window"
              },
              @{
                  @"tag":@"loading2",
                  @"title":@"展示loading+Text：默认window"
              },
              @{
                  @"tag":@"loading3",
                  @"title":@"展示loading+Text：指定View-self.view"
              },
              @{
                  @"tag":@"loading3.1",
                  @"title":@"展示loading+Text：指定View-self.view。展示宽限时间0.1s"
              },
              @{
                  @"tag":@"success1",
                  @"title":@"提示Text：默认window"
              },
              @{
                  @"tag":@"success1.1",
                  @"title":@"提示Text：默认window，toast展示时间根据文本长度动态计算"
              },
              @{
                  @"tag":@"success1.2",
                  @"title":@"提示Text：默认window，toast展示时间最大6s，最小1.5s"
              },
              @{
                  @"tag":@"success2",
                  @"title":@"提示Text：指定View-self.view"
              },
              @{
                  @"tag":@"success3",
                  @"title":@"提示Text：指定View-self.view，指定延迟时间4s"
              },
              @{
                  @"tag":@"success4",
                  @"title":@"提示Text：指定View-self.view，指定延迟时间4s，指定完成block回调"
              },
              @{
                  @"tag":@"success5",
                  @"title":@"提示Text：默认window。支持线程安全，子线程调用也安全"
              },
        ]},
    ];
    self.dataArray = sectionArr;
}


#pragma mark - TableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    NSArray *list = [dic objectForKey:@"list"];
    return list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = [list objectAtIndex:indexPath.row];
    NSString *value = [rowDic objectForKey:@"title"];
    cell.textLabel.text = value;
    cell.textLabel.numberOfLines = 0;
    return cell;
}

///不支持换行的
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    NSDictionary *dic =  [self.dataArray objectAtIndex:section];
//    NSString *title = [dic objectForKey:@"sectionTitle"];
//    return title;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CHSHeaderTitleView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([CHSHeaderTitleView class])];
    NSDictionary *dic = [self.dataArray objectAtIndex:section];
    headerView.titleLab.text = [NSString stringWithFormat:@"%@.%@",@(section+1),[dic objectForKey:@"sectionTitle"]];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return tableView.estimatedSectionHeaderHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *list = [dic objectForKey:@"list"];
    NSDictionary *rowDic = [list objectAtIndex:indexPath.row];
    NSString *tag = [rowDic objectForKey:@"tag"];
    if ([tag isEqualToString:@"loading1"]) {
        [MBProgressHUD zx_showLoading];
        [self performSelector:@selector(automicHideHUD) withObject:nil afterDelay:2];
    }
    else if ([tag isEqualToString:@"loading2"]) {
        [MBProgressHUD zx_showLoadingWithStatus:@"正在加载"];
        [self performSelector:@selector(automicHideHUD) withObject:nil afterDelay:2];
    }
    else if ([tag isEqualToString:@"loading3"]) {
        [MBProgressHUD zx_showLoadingWithStatus:@"正在加载zx_showLoadingWithStatuszx_showLoadingWithStatus" toView:self.view];
    }
    else if ([tag isEqualToString:@"loading3.1"]) {
        [MBProgressHUD zx_showLoadingWithStatus:@"正在加载"];
        [self performSelector:@selector(automicHideHUD) withObject:nil afterDelay:0.1];
    }
    else if ([tag isEqualToString:@"success1"]) {
        [MBProgressHUD zx_showError:@"删除成功"];
    }
    else if ([tag isEqualToString:@"success1.1"]) {
        [MBProgressHUD zx_showError:@"toast展示时间根据文本长度动态计算"];
    }
    else if ([tag isEqualToString:@"success1.2"]) {
        [MBProgressHUD zx_showError:@"隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算隐藏时间根据文本长度动态计算"];
    }

    else if ([tag isEqualToString:@"success2"]) {
        [MBProgressHUD zx_showError:@"zx_showLoadingWithStatuszx_showLoadingWithStatus" toView:self.view];
    }
    else if ([tag isEqualToString:@"success3"]) {
        [MBProgressHUD zx_showError:@"zx_showLoadingWithStatuszx_showLoadingWithStatus" toView:self.view hideAfterDelay:4];
    }
    else if ([tag isEqualToString:@"success4"]) {
        [MBProgressHUD zx_showError:@"zx_showLoadingWithStatuszx_showLoadingWithStatus" toView:self.view hideAfterDelay:4 completionBlock:^{
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.textLabel.textColor = [UIColor redColor];
        }];
    }
    else if ([tag isEqualToString:@"success5"]) {
        [NSThread detachNewThreadWithBlock:^{
            [MBProgressHUD zx_showError:@"zx_showLoadingWithStatuszx_showLoadingWithStatus"];
        }];
    }
    
}

- (void)automicHideHUD{
    [MBProgressHUD zx_hideHUD];

}

- (IBAction)hideHUDAction:(id)sender {
    
    [MBProgressHUD zx_hideHUDForView:self.view];
}
@end
